# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/8 23:00
# @Author   : Robot-Zsj
"""
description:

input
3 2
yuwen shuxue
fangfang 95 90
xiaohua 88 95
minmin 100 82
shuxue
output
xiaohua fangfang minmin
"""


class Student:
    def __init__(self, name, rank):
        self.name = name
        self.rank = rank


n, m = map(int, input().split())

subject_ranIdx = {}

subjects = input().split()

for i in range(m):
    subject_ranIdx[subjects[i]] = i

students = []
for i in range(n):
    tmp = input().split()

    name = tmp[0]
    scores = list(map(int, tmp[1:]))

    rank = []
    rank.extend(scores)
    rank.append(sum(scores))

    students.append(Student(name, rank))

subject = input()

rankIdx = subject_ranIdx.get(subject, m)

students.sort(key=lambda x: (-x.rank[rankIdx], x.name))

print(" ".join(map(lambda x: x.name, students)))
